<template>

  <div class="cardManager">
    <div class="search">
      <el-form ref="form" :model="searchParam" label-width="80px" inline>
        <el-form-item label="标题">
          <el-input v-model="searchParam.name"></el-input>
        </el-form-item>


        <el-form-item label="创建时间">
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择日期" v-model="searchParam.startTime" style="width: 100%;" format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2">-</el-col>
          <el-col :span="11">
            <el-date-picker type="datetime" placeholder="选择时间" v-model="searchParam.endTime" style="width: 100%; " format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
          </el-col>
        </el-form-item>
        <div class="sub">
          <el-button type="success" @click="chageData(true)">新增</el-button>
          <el-button type="primary" @click="getEssay">查询</el-button>
          <el-button>取消</el-button>
        </div>

      </el-form>
    </div>
    <div class="dataTable">
      <el-table :data="essayList"  border>
        <el-table-column type="index" label="序号" width="120"/>
        <el-table-column  label="标题" width="120" prop="title">

        </el-table-column>
        <el-table-column label="内容"  width="400" >
          <template slot-scope="scope">
            <span v-html="scope.row.content                                                                                                                            "></span>
          </template>
        </el-table-column>
        <el-table-column label="是否可用"  width="120">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.isView"
                active-color="#13ce66"
                disabled
                inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createAt" label="时间"  width="200"/>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="editCardInfo(scope.row)">编辑</el-button>
            <el-button type="danger" v-text="scope.row.isView?'禁用':'启用'" @click="chageStatus(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">

        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page=searchParam.queryPageDTO.currentPage
            :page-sizes="[10, 50, 100, 200]"
            :page-size=searchParam.queryPageDTO.pageSize
            layout="total, sizes, prev, pager, next, jumper"
            :total="essayListSize">
        </el-pagination>
      </div>
    </div>

    <div>
      <el-dialog
          title="添加和修改"
          :visible.sync="dialogVisible"
          width="50%"
          center>
        <AddContent :essay="essayObj" @chageData="chageData"></AddContent>
      </el-dialog>
    </div>

  </div>

</template>

<script>

import AddContent from "@c/common/AddContent";
import {Message} from "element-ui";
export default {
  components: {AddContent},
  data(){
    return{
      searchParam:{
        cardId: 0,
        queryPageDTO: {
          currentPage: 1,
          pageSize: 10
        }
      },
      essayListSize:null,
      essayList:[],
      cardList:[],
      dialogVisible:false,
      essayObj:{},
      id:'',
      name:'',
    }
  },
  methods:{
    getEssay (){
      this.searchParam.cardId = this.id
      this.postRequestJson("/api/portal/getEssayByPage", this.searchParam).then(resp=>{
        this.essayList= resp.data.data
        this.essayListSize = resp.data.size
      })
    },
    getDictTypes() {

      this.postRequestJson("/api/tg/portal/getCardType",{}).then(resp=>{
        this.cardTypeList = resp.data.data
      })
    },
    editCardInfo(data) {
      this.dialogVisible = true
      this.essayObj = data
    },
    chageData(flag) {
      this.essayObj ={}
      this.essayObj.isView = true
      this.essayObj.cardId = this.id
      this.dialogVisible = flag;
      if (!flag) {
        this.getEssay()
      }
    },
    chageStatus(data) {
      this.$confirm('是否改变状态', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        data.isView = !data.isView
        this.postRequestJson("api/tg/essay/addOrEditEssay",data).then(resp=>{
          if (resp.data.code === '0') {
            Message.success("操作成功")
          }
        })
      })
    },
    toContent(id){
      this.$router.push({
        path:'/portal/content',
        query:{id:id}})
    },
    handleSizeChange(val) {
      this.searchParam.queryPageDTO.pageSize = val
      this.getEssay()
    },

    handleCurrentChange(val){
      this.searchParam.queryPageDTO.currentPage = val
      this.getEssay()
    },


  },
  created() {
    this.id = this.$route.query.id
    this.name = this.$route.query.name
    this.getEssay()
    this.getDictTypes()

  },

}
</script>
<style>
.cardManager{
  margin-left: 50px;
}
.cardManager .search{
  padding: 30px;
  margin-top: 100px;
  border: #d4caca 1px solid;
}
.cardManager .search .sub{
  margin-top: 60px;
}
.cardManager .dataTable{
  margin-top: 60px;
}

</style>